package com.dayuan.utils;

import java.util.ArrayList;
import java.util.List;
/*								 _ooOoo_                           
				       			o8888888o                          
				       			88" . "88                            
				       			(| -_- |)                           
				       			O\  =  /O                         
			            	 ____/`---'\____                       
				  		   .'  \\|     |//  `.                     
				  		  /  \\|||  :  |||//  \                     
						 /  _||||| -:- |||||-  \                     
						 |   | \\\  -  /// |   |                     
						 | \_|  ''\---/''  |   |                     
						 \  .-\__  `-`  ___/-. /                   
			       	   ___ `. .'  /--.--\  `. . __               
			   		 ."" '<  `.___\_<|>_/___.'  >'"".               
		            | | :  `- \`.;`\ _ /`;.`/ - ` : | |               
			  	    \  \ `-.   \_ __\ /__ _/   .-` /  /          
		       ======`-.____`-.___\_____/___.-`____.-'======                            
								  `=---='          
		      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
		      			佛珠保佑			永无bug
		      			已用				不要删除
		      			
		      									*/

public class pageHelper {
	/**
	 * 总行数
	 */
	private int totalCount;
	/**
	 * 单页行数
	 */
	private int pageNum = 5;
	/**
	 * 当前页
	 */
	private int currentPageIndex = 1;
	/**
	 * 总页数
	 */
	private int totalPages;
	/**
	 * 开始索引
	 */
	private int startIndex = 0;

	/**
	 * 页码列表大小，默认5
	 */
	private int pageListSize = 5;

	/**
	 * 页码列表
	 */
	private List<Integer> pageNumList = new ArrayList<Integer>();

	public pageHelper() {
		// TODO Auto-generated constructor stub
	}

	public pageHelper(Integer currentPageIndex, Integer totalCount) {
		this.currentPageIndex = currentPageIndex;
		this.totalCount = totalCount;

		this.startIndex = getStartIndex();
		getTotalPages();
		getPageNumList();
	}

	public Integer getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getCurrentPageIndex() {
		return currentPageIndex;
	}

	public void setCurrentPageIndex(int currentPageIndex) {
		this.currentPageIndex = currentPageIndex;
	}

	/**
	 * 计算总页数
	 * 
	 * @return
	 */
	public int getTotalPages() {
		if (totalCount == 0) {
			totalPages = 0;
			return totalPages;
		}

		if (totalCount % pageNum == 0) {
			totalPages = totalCount / pageNum;
		} else {
			totalPages = totalCount / pageNum + 1;
		}
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	/**
	 * 计算开始索引
	 */
	public Integer getStartIndex() {
		return (currentPageIndex - 1) * pageNum;
	}

	public void setStartIndex(Integer startIndex) {
		this.startIndex = startIndex;
	}

	// 得到页面列表
	public List<Integer> getPageNumList() {
		this.pageNumList.removeAll(this.pageNumList);// 设置之前先清空
		if (totalPages > this.pageListSize) {
			int halfSize = this.pageListSize / 2;
			int first = 1;
			int end = 1;
			if (this.currentPageIndex - halfSize < 1) { // 当前页靠近最小数1
				first = 1;
				end = this.pageListSize;
			} else if (totalPages - this.currentPageIndex < halfSize) { // 当前页靠近最大数
				first = totalPages - this.pageListSize + 1;
				end = totalPages;
			} else {
				first = this.currentPageIndex - halfSize;
				end = this.currentPageIndex + halfSize;
			}
			for (int i = first; i <= end; i++) {
				this.pageNumList.add(i);
			}
		} else {
			for (int i = 0; i < totalPages; i++) {
				this.pageNumList.add(i + 1);
			}
		}

		return pageNumList;
	}

	public int getPageListSize() {
		return pageListSize;
	}

	public void setPageListSize(int pageListSize) {
		this.pageListSize = pageListSize;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}

	public void setPageNumList(List<Integer> pageNumList) {
		this.pageNumList = pageNumList;
	}
	

}
